class Player {
    constructor(name, isBot = false) {
        this.name = name;
        this.isBot = isBot;
        this.cards = [];
        this.isLandlord = false;
    }

    addCards(cards) {
        this.cards = this.cards.concat(cards);
        this.sortCards();
    }

    removeCards(cards) {
        cards.forEach(card => {
            const index = this.cards.findIndex(c => 
                c.suit === card.suit && c.value === card.value
            );
            if (index !== -1) {
                this.cards.splice(index, 1);
            }
        });
    }

    sortCards() {
        this.cards.sort((a, b) => b.value - a.value);
    }

    // AI决策逻辑（简单版本）
    makeMove(lastPlay) {
        if (this.isBot) {
            // 如果是第一手，出最小的单张
            if (!lastPlay || lastPlay.length === 0) {
                return [this.cards[this.cards.length - 1]];
            }
            
            // 尝试找到相同牌型且更大的牌
            const lastType = Rules.getCardType(lastPlay);
            const possiblePlays = this.findPossiblePlays(lastType, lastPlay.length);
            
            for (let play of possiblePlays) {
                if (Rules.canBeat(play, lastPlay)) {
                    return play;
                }
            }
            
            // 如果找不到合适的牌，尝试出炸弹
            const bomb = this.findBomb();
            if (bomb) return bomb;
            
            return [];  // 不出
        }
        return null;  // 人类玩家返回null
    }

    findPossiblePlays(type, length) {
        let plays = [];
        
        // 根据不同牌型查找可能的出牌
        switch (type) {
            case Rules.CARD_TYPES.SINGLE:
                this.cards.forEach(card => plays.push([card]));
                break;
            case Rules.CARD_TYPES.PAIR:
                for (let i = 0; i < this.cards.length - 1; i++) {
                    if (this.cards[i].value === this.cards[i + 1].value) {
                        plays.push([this.cards[i], this.cards[i + 1]]);
                    }
                }
                break;
            // 可以继续添加其他牌型的查找逻辑
        }
        
        return plays;
    }

    findBomb() {
        for (let i = 0; i < this.cards.length - 3; i++) {
            if (this.cards[i].value === this.cards[i + 1].value &&
                this.cards[i + 1].value === this.cards[i + 2].value &&
                this.cards[i + 2].value === this.cards[i + 3].value) {
                return this.cards.slice(i, i + 4);
            }
        }
        return null;
    }
} 